--[[
    提供服务端座位号和客户端座位号转换
    客户端自己的座位号永远是0，其他玩家依次为：
    1-左上角
    2-左下角
    3-右上角
    4-右下角
]]

local blaze = require "blaze"

local M = {}

local CLIENT_MAX_SEAT_NO = 4
local SERVER_MAX_SEAT_NO = 5
local c2s = {} -- <客户端座位号, 服务端座位号>
local s2c = {} -- <服务端座位号, 客户端座位号>
local inited = false

local function repeat_map(no)
    while no > SERVER_MAX_SEAT_NO do
        no = no - SERVER_MAX_SEAT_NO
    end
    return no
end

local function init()
    local player = blaze.kbe.player()
    if not player or not player.seat then
        return
    end
    local my_no = player.seat.no
    c2s[0] = my_no
    s2c[my_no] = 0
    for c=1, CLIENT_MAX_SEAT_NO do
        local s = repeat_map(my_no + c)
        c2s[c] = s
        s2c[s] = c
    end
    -- print("[DEBUG]s2c=", blaze.dump(s2c))
    -- print("[DEBUG]c2s=", blaze.dump(c2s))
end

function M.to_local(server_no)
    if not inited then
        init()
    end
    return s2c[server_no] or 0
end

function M.to_server(local_no)
    if not inited then
        init()
    end
    return c2s[local_no] or 0
end

function M.reset()
    inited = false
end

function M.dump()
    return blaze.dump({
        inited = inited,
        c2s = c2s,
        s2c = s2c
    })
end

return M